package com.youlai.boot.modules.exhibition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.modules.exhibition.mapper.ExhibitionBrandMapper;
import com.youlai.boot.modules.exhibition.service.ExhibitionBrandService;
import com.youlai.boot.modules.exhibition.model.ExhibitionBrand;
import com.youlai.boot.modules.exhibition.model.ExhibitionBrandForm;
import com.youlai.boot.modules.exhibition.model.ExhibitionBrandQuery;
import com.youlai.boot.modules.exhibition.model.ExhibitionBrandVO;
import com.youlai.boot.modules.exhibition.converter.ExhibitionBrandConverter;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 展会品牌关联服务实现类
 *
 * @author maoyu
 * @since 2025-04-17 14:53
 */
@Service
@RequiredArgsConstructor
public class ExhibitionBrandServiceImpl extends ServiceImpl<ExhibitionBrandMapper, ExhibitionBrand> implements ExhibitionBrandService {

    private final ExhibitionBrandConverter exhibitionBrandConverter;

    /**
    * 获取展会品牌关联分页列表
    *
    * @param query 查询参数
    * @return {@link IPage<ExhibitionBrandVO>} 展会品牌关联分页列表
    */
    @Override
    public IPage<ExhibitionBrandVO> getExhibitionBrandPage(ExhibitionBrandQuery query) {
        Page<ExhibitionBrandVO> pageVO = this.baseMapper.getExhibitionBrandPage(
                new Page<>(query.getPageNum(), query.getPageSize()),
                query
        );
        return pageVO;
    }

    /**
     * 获取展会品牌关联表单数据
     *
     * @param id 展会品牌关联ID
     * @return
     */
    @Override
    public ExhibitionBrandForm getExhibitionBrandFormData(Long id) {
        ExhibitionBrand entity = this.getById(id);
        return exhibitionBrandConverter.toForm(entity);
    }

    /**
     * 新增展会品牌关联
     *
     * @param formData 展会品牌关联表单对象
     * @return
     */
    @Override
    public boolean saveExhibitionBrand(ExhibitionBrandForm formData) {
        ExhibitionBrand entity = exhibitionBrandConverter.toEntity(formData);

        boolean exists = this.lambdaQuery().eq(ExhibitionBrand::getExhibitionId, entity.getExhibitionId()).eq(ExhibitionBrand::getBrandId, entity.getBrandId()).exists();
        if (!exists) {
            return this.save(entity);
        }

        return false;
    }

    /**
     * 更新展会品牌关联
     *
     * @param id   展会品牌关联ID
     * @param formData 展会品牌关联表单对象
     * @return
     */
    @Override
    public boolean updateExhibitionBrand(Long id,ExhibitionBrandForm formData) {
        ExhibitionBrand entity = exhibitionBrandConverter.toEntity(formData);
        return this.updateById(entity);
    }

    /**
     * 删除展会品牌关联
     *
     * @param ids 展会品牌关联ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteExhibitionBrands(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的展会品牌关联数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
